<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_readm_title']);

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$msgaction = (isset($_GET['msgaction'])) ? $_GET['msgaction'] : '';
$id = (isset($_GET['id'])) ? $_GET['id'] : '';

if ($msgaction == "delete")
{
	$result = $smcFunc['db_query']('', '
		DELETE FROM {raw:db_messages}
		WHERE ID = {int:id}
		AND recp_id = {int:ship_id}',
		array(
			'db_messages' => $dbtables['messages'],
			'id' => $id,
			'ship_id' => $playerinfo['ship_id']
		)
	);
}
elseif ($msgaction == "delete_all")
{
	$result = $smcFunc['db_query']('', '
		DELETE FROM {raw:db_messages}
		WHERE recp_id = {int:ship_id}',
		array(
			'db_messages' => $dbtables['messages'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
}

$cur_D = date("Y-m-d");
$cur_T = date("H:i:s");

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_messages}
	WHERE recp_id = {int:ship_id}
	ORDER BY sent DESC',
	array(
		'db_messages' => $dbtables['messages'],
		'ship_id' => $playerinfo['ship_id']
	)
);

echo '
		<div align="center">
			<table border="0" cellspacing="0" width="70%" bgcolor="silver" cellpadding="0">
				<tr>
					<td width="100%">
						<div align="center">
							<center>
							<table border="0" cellspacing="1" width="100%">
								<tr>
									<td width="100%" bgcolor="black">
										<div align="center">
											<table border="1" cellspacing="1" width="100%" bgcolor="gray" bordercolorlight="black" bordercolordark="silver">
												<tr>
													<td width="75%" align="left"><font color="white" size="2"><b>', $txt['bnt_readm_center'], '</b></font></td>
													<td width="21%" align="center" nowrap><font color="white" size="2">', $cur_D, '&nbsp;', $cur_T, '</font></td>
													<td width="4%" align="center" bordercolorlight="black" bordercolordark="gray"><a href="', $scripturl, '?action=blacknova;sa=main"><img alt="Click here to return to the main menu" src="', $settings['images_url'], '/bnt/c95x.gif" width="16" height="14" border="0"></a></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>';

if ($smcFunc['db_num_rows']($result) == 0)
{
//  echo "$l_readm_nomessage";
	echo '
								<tr>
									<td width="100%" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="1" cellspacing="1" width="100%" bgcolor="white" bordercolorlight="black" bordercolordark="silver">
												<tr>
													<td width="100%" align="center" bgcolor="white"><font color="red">', $txt['bnt_readm_nomessage'], '</font></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>';
}
else
{
	$line_counter = true;
	while ($msg = $smcFunc['db_fetch_assoc']($result))
	{
		$shipresult = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_ships}
			WHERE ship_id = {int:sender_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'sender_id' => $msg['sender_id']
			)
		);
		$sender = $smcFunc['db_fetch_assoc']($shipresult);
		$smcFunc['db_free_result']($shipresult);
		
		echo '
								<tr>
									<td width="100%" align="center" bgcolor="black" height="4"></td>
								</tr>
								<tr>
									<td width="100%" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="0" cellspacing="1" width="100%" bgcolor="gray" cellpadding="0">
												<tr>
													<td width="20%"><font color="white" size="2"><b>', $txt['bnt_readm_sender'], '</b></td>
													<td width="55%"><font color="yellow" size="2">', $sender['character_name'], '</font></td>
													<td width="21%" align="center"><font color="white" size="2">', $msg['sent'], '</font></td>
													<td width="4%" align="center" bordercolorlight="black" bordercolordark="gray"><a class="but" HREF="', $scripturl, '?action=blacknova;sa=readmail;msgaction=delete;id=', $msg['ID'], '"><img src="', $settings['images_url'], '/bnt/c95x.gif" width="16" height="14" border="0"></a></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>
								<tr>
									<td width="100%" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="0" cellspacing="1" width="100%" bgcolor="gray" cellpadding="0">
												<tr>
													<td width="20%"><font color="white" size="2"><b>', $txt['bnt_readm_captn'], '</b></font></td>
													<td width="80%"><font color="yellow" size="2">', $sender['ship_name'], '</font></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>
								<tr>
									<td width="100%" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="0" cellspacing="1" width="100%" bgcolor="gray" cellpadding="0">
												<tr>
													<td width="20%"><font color="white" size="2"><b>', $txt['bnt_mt_subject'], '</b></font></td>
													<td width="80%"><b><font color="yellow" size="2">', $msg['subject'], '</font></b></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>
								<tr>
									<td width="100%" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="1" cellspacing="1" width="100%" bgcolor="white" bordercolorlight="black" bordercolordark="silver">
												<tr>
													<td width="100%"><font color="black" size="2">', nl2br($msg['message']), '</font></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>
								<tr>
									<td width="100%" align="center" bgcolor="black" bordercolorlight="black" bordercolordark="silver">
										<div align="center">
											<table border="1" cellspacing="1" width="100%" bgcolor="gray" bordercolorlight="black" bordercolordark="silver" cellpadding="0">
												<tr>
														<td width="100%" align="center" valign="middle">
															<a class="but" href="', $scripturl, '?action=blacknova;sa=readmail;msgaction=delete;id=', $msg['ID'], '">', $txt['bnt_readm_del'], '</a> |
															<a class="but" href="', $scripturl, '?action=blacknova;sa=mailto2;to=', $sender['character_name'], ';subject=', $msg['subject'], '">', $txt['bnt_readm_repl'], '</a>
														</td>
												</tr>
											</table>
										</div>
									</td>
								</tr>';
	}
	$smcFunc['db_free_result']($result);
}
echo '
								<tr>
									<td width="100%" align="center" bgcolor="black" height="4"></td>
								</tr>
								<tr>
									<td width="100%" align="center" bgcolor="#000000" height="4">
										<div align="center">
											<table border="1" cellspacing="1" width="100%" bgcolor="#808080" bordercolorlight="#000000" bordercolordark="#C0C0C0" height="8">
												<tr>
													<td width="50%"><p align="left"><font color="#FFFFFF" size="2">', $txt['bnt_readm_reader'], '</font></td>
													<td width="50%"><p align="right"><font color="#FFFFFF" size="2"><a class="but" href="', $scripturl, '?action=blacknova;sa=readmail;msgaction=delete_all">', $txt['bnt_readm_delall'], '</a></font></td>
												</tr>
											</table>
										</div>
									</td>
								</tr>
							</table>
						</center>
					</div>
				</td>
			</tr>
		</table>
	</div>
	<br>';

TextMainMenu();
?>
